package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class BookService {
    public static List<String> bookMenu = new ArrayList<String>();

    public BookService() {
        bookMenu.add("CheckoutBook");
        bookMenu.add("ReturnBook");
        bookMenu.add("BackMainmenu");
        bookMenu.add("Quit");
    }

    public String checkoutBook(int id) {
        String result = "Error input!Please try again!";
        if (id != -1) {
            Book book = getBookById(id);
            if (book != null && book.getStatus() == 0) {
                result = "Thank you! Enjoy the book.";
                book.setStatus(1);
            } else {
                result = "That book is not available.";
            }
        }
        return result;
    }

    public String returnBook(int id) {
        String result = "Error input!Please try again!";
        if (id != -1) {
            Book book = getBookById(id);
            if (book != null && book.getStatus() == 1) {
                result = "Thank you for returning the book.";
                book.setStatus(0);
            } else {
                result = "That is not a valid book to return.";
            }
        }
        return result;
    }

    public Book getBookById(int id) {
        for (Book book : Library.bookList) {
            if (book.getBookId() == id) {
                return book;
            }
        }
        return null;
    }

    public void printAllBooks() {
        System.out.println("*******************************************************************************");
        System.out.println("                                     Book  List                     ");
        System.out.println("bookId" + "    " + "bookName" + "                         " + "author" + "      " + "yearPublished" + "    " + "satus");
        for (Book book : Library.bookList) {
            if (book.getStatus() == 0)
                System.out.println(book.toString());
        }
    }


    public void bookMenu() {
        System.out.print("BookMenu:*");
        for (String s : bookMenu) {
            System.out.print("   " + s + "   *");
        }
        System.out.println();
        System.out.println("*******************************************************************************");

    }
}
